package club.emergency.wechat.manager.search.consumer.controller;

import club.emergency.wechat.common.response.StatusCode;
import club.emergency.wechat.common.response.ResponseEntity;
import club.emergency.wechat.common.utils.JsonConvert;
import club.emergency.wechat.manager.search.consumer.service.ManagerSearchConsumer;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

/**
 * Create with IntelliJ IDEA
 * Project Name: wechat
 * Package Name: club.emergency.wechat.manager.search.consumer.controller
 * Date: 2018/12/5
 *
 * @author: 快乐的小菌菌
 * Description:
 */
@RestController
@Api("管理员查询接口")
@RequestMapping("/manager-search")
public class ManagerSearchController {
    private ManagerSearchConsumer managerSearchConsumer;

    public ManagerSearchController(ManagerSearchConsumer staffSearchConsumer) {
        this.managerSearchConsumer = staffSearchConsumer;
    }

    @ApiOperation("按条件查询管理员列表")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "当前页", name = "pageNo", dataType = "int", paramType = "query"),
            @ApiImplicitParam(value = "每页显示最大数据量", name = "pageSize", dataType = "int", paramType = "query")
    })
    @PostMapping("/flipList")
    public ResponseEntity flipList(
            @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize
    ) {
        return new JsonConvert<ResponseEntity>().jsonDecode(managerSearchConsumer.flipList(pageNo, pageSize), ResponseEntity.class);
    }

    @ApiOperation("登录")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "用户名", name = "userName", dataType = "string", paramType = "query"),
            @ApiImplicitParam(value = "密码", name = "password", dataType = "string", paramType = "query"),
    })
    @PostMapping("/login")
    public ResponseEntity login(
            @RequestParam("userName") String userName,
            @RequestParam("password") String password,
            HttpServletResponse httpServletResponse
    ) {
        String login = managerSearchConsumer.login(userName, password);
        ResponseEntity responseEntity = new JsonConvert<ResponseEntity>().jsonDecode(login, ResponseEntity.class);
        if (responseEntity.getStatusCode() != StatusCode.LOGIN_OK) {
            return responseEntity;
        }
        //把数据放在authorization中
        httpServletResponse.setHeader("Authorization", new JsonConvert<>().jsonEncode(responseEntity.getData()));
        return responseEntity;
    }

    @ApiOperation("更新Token")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "id", name = "id", dataType = "Long", paramType = "query"),
    })
    @PostMapping("/updateToken")
    public ResponseEntity updateToken(
            @RequestParam("id") Long id,
            HttpServletResponse httpServletResponse
    ) {
        String updateToken = managerSearchConsumer.updateToken(id);
        ResponseEntity responseEntity = new JsonConvert<ResponseEntity>().jsonDecode(updateToken, ResponseEntity.class);
        if (responseEntity.getStatusCode() != StatusCode.LOGIN_OK) {
            return responseEntity;
        }
        //把数据放在authorization中
        httpServletResponse.setHeader("Authorization", new JsonConvert<>().jsonEncode(responseEntity.getData()));
        return responseEntity;
    }
}
